package com.situ.stl.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;

import java.io.Serializable;
@Data
public class Result implements Serializable {
    private Integer code;
    private String msg;
    private Object data;
    public static Result success(Integer code, String msg, Object data) {
        Result result = new Result();
        result.setCode(code);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }
    public static Result success(Integer code, String msg) {
        return Result.success(code,msg, null);
    }
    public static Result success(Integer code) {
        return Result.success(code,null, null);
    }
    public static Result success(Integer code, Object data) {
        return Result.success(code,null, data);
    }
    public static Result error(Integer code, String msg) {
        Result result = new Result();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    /**
     * 将当前对象转成json格式的字符串
     * @return
     * @throws JsonProcessingException
     */
    public String toJson() throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(this);
    }
}
